package com.unit10.DAO;

import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.ArrayList;

import com.unit10.pojo.Emp;
import com.unit10.utils.*;

public class EmpDAO {
    //查询数据公共方法
    public ArrayList findEmpBySql(String sql) throws SQLException {
        ArrayList<Emp> empsList=new ArrayList<Emp>();
        Connection conn=GetConnection.getConn();//调用获取连接的方法
        Statement stmt=conn.createStatement();
        ResultSet rs=stmt.executeQuery(sql);//将执行结果使用ResultSet接收
        while(rs.next()) {
            Emp emp=new Emp();
            emp.setId(rs.getInt("id"));
            emp.setName(rs.getNString("name"));
            emp.setJob(rs.getString("job"));
            emp.setAge(rs.getInt("age"));
            emp.setHireDate(rs.getDate("hireDate"));
            emp.setSalary(rs.getDouble("salary"));
            emp.setDeptName(rs.getString("deptName"));
            empsList.add(emp);
        }
        conn.close();
        stmt.close();
        return empsList;
    }

    //增加和删除
    public void updateEmpBySql(String sql) throws SQLException {

        Connection conn=GetConnection.getConn();
        Statement stmt=conn.createStatement();
        stmt.execute(sql);
//        System.out.println("成功添加");
        conn.close();
        stmt.close();
    }

}
